﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Xml;
using System.Text;
using System.Text.RegularExpressions;
using CMSModel;
using System.Data;

/// <summary>
///合作伙伴 获取数据信息
/// </summary>
public class HzhbCatch
{

    public void Import()
    {
        try
        {
            //获取数据地址和typeid            
            string str_xml = HttpContext.Current.Server.MapPath("~/NewCatch/Hzhb.xml");
            DataSet ds = new DataSet();
            //从xml中读取数据
            ds.ReadXml(str_xml);
            if (ds != null)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string url = ds.Tables[0].Rows[i][1].ToString();
                    string typeid = ds.Tables[0].Rows[i][0].ToString();
                    if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(typeid))
                    {
                        Import(url, typeid);
                    }
                }
            }
        }
        catch
        { }
    }

    string urlReg = "<item>[\\S\\s]*?</item>";
    //string desReg = "<p><a href=\"http://www.fx168.com\">FX168首页??/a>><a href=\"http://www.fx168.com/fxnews/news_now_html.html\">所有新??/a>>正文<p/>";
    string desReg = "<p>[\\S\\s]*?<p/>";
    public void Import(string dataurl, string typeid)
    {
        try
        {
            WebClient client = new WebClient();
            client.Headers.Add("Content-Type", "text/html;charset=utf-8");
            Byte[] pageData = client.DownloadData(dataurl);
            string str = Encoding.UTF8.GetString(pageData);

            MatchCollection mc = Regex.Matches(str, urlReg, RegexOptions.None);
            CMSEntities cms = new CMSEntities();
            for (int i = 0; i < mc.Count; i++)
            {

                //逐行取值
                XmlDocument doc = new XmlDocument();
                string tt = mc[i].Value;
                doc.LoadXml(tt);
                var root = doc.DocumentElement;
                var title = root.ChildNodes[0].InnerText;
                var link = root.ChildNodes[1].InnerText;
                var pubdate = root.ChildNodes[2].InnerText;
                var author = root.ChildNodes[3].InnerText;
                var des = root.ChildNodes[4].InnerText;
                //des = des.Replace(desReg, "");
                des = Regex.Replace(des, desReg, "");

                if (IsCheck(link))
                {
                    continue;
                }

                //保存新闻

                News news = new News();
                news.Source = author;
                news.step = 0;
                news.content = des;
                news.ctime = DateTime.Now;
                news.ptime = DateTime.Parse(pubdate);
                news.title = title;
                news.keyword = title;
                //大于50个字的才发布
                if(des.Length>=50)
                    news.IsEnable = true;
                News_Type newsType = new News_Type();
                newsType.NewsTypeId = typeid;
                newsType.ctime = DateTime.Now;
                news.News_Type.Add(newsType);
                cms.News.AddObject(news);
                cms.SaveChanges();
                //保存catch数据
                NewsCatch nCatch = new NewsCatch();
                nCatch.url = link;
                nCatch.NewsID = news.id;
                nCatch.CTime = DateTime.Now;
                cms.NewsCatch.AddObject(nCatch);
                cms.SaveChanges();



            }
        }
        catch (Exception ex)
        { }
    }

    /// <summary>
    /// 检查是否已导入该新闻
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    bool IsCheck(string url)
    {
        CMSEntities cms = new CMSEntities();
        return cms.NewsCatch.Any(x => x.url == url);
    }

    public HzhbCatch()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //

    }
}